//
//  DWKVOPerson.m
//  OCAPPDemo
//
//  Created by 杨冬武 on 2021/5/17.
//

#import "DWKVOPerson.h"

@implementation DWKVOPerson
//@synthesize age = _age;
//@dynamic isAge;
//@synthesize isAge = isAge;
//@synthesize _age, _isAge, age, isAge;

#pragma mark - KVC


//-------get查找顺序

- (int)getAge {
    NSLog(@"%s", __FUNCTION__);
    return 10;
}

//- (int)age {
//    NSLog(@"%s", __FUNCTION__);
//    return _age;
//}

//- (int)isAge {
//    NSLog(@"%s", __FUNCTION__);
//    return 10;
//}

//- (int)_age {
//    NSLog(@"%s", __FUNCTION__);
//    return 20;
//}


//-------set查找顺序

- (void)setAge:(int)age {
//    _age = age;
    NSLog(@"%s", __FUNCTION__);
}

//- (void)_setAge:(int)age {
//    //_age = age;
//    NSLog(@"%s", __FUNCTION__);
//}


/// 是否允许KVC直接访问成员变量
+ (BOOL)accessInstanceVariablesDirectly {
    return NO;
}

- (void)printPersion {
//    NSLog(@"_age %d", _age);
//    NSLog(@"_isAge %d", _isAge);
//    NSLog(@"age %d", age);
//    NSLog(@"isAge %d", isAge);
}


#pragma mark - KVO
@synthesize fullName = _fullName;

//- (NSString *)fullName {
//    return [NSString stringWithFormat:@"%@·%@", self.firstName, self.lastName];
//}

//- (void)setFullName:(NSString *)fullName {
//    _fullName = [fullName copy];
//
//    NSArray *nameArr = [fullName componentsSeparatedByString:@"·"];
//    _firstName = nameArr.firstObject;
//    _lastName = nameArr.lastObject;
//}

/// 键值依赖
/// @param key 键

+ (NSSet<NSString *> *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
    
//    if ([key isEqualToString:@"firstName"] || [key isEqualToString:@"lastName"]) {
//        return [NSSet setWithObject:@"fullName"];
//    }
    
    if ([key isEqualToString:@"fullName"]) {
        return [NSSet setWithObjects:@"firstName", @"lastName", nil];;
    }
    
    return [super keyPathsForValuesAffectingValueForKey:key];
    
    
//    NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
//    if ([key isEqualToString:@"fullName"]) {
//        NSArray *affectingKeys = @[@"lastName", @"firstName"];
//        keyPaths = [keyPaths setByAddingObjectsFromArray:affectingKeys];
//    }
//    return keyPaths;
    
}
 

/// 指定键的键值依赖
//+ (NSSet<NSString *> *)keyPathsForValuesAffectingFullName {
//    return [NSSet setWithObjects:@"firstName", @"lastName", nil];
//}


/// 键值禁用
/// @param key 键
//+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
//    // 此方法返回值为YES是允许,返回NO为不允许,可以根据key返回不同键
//    if ([key isEqualToString:@"firstName"]) {
//        return  NO;
//    }
//    return YES;
//}


/// 指定key的键值禁用
//+ (BOOL)automaticallyNotifiesObserversOfLastName {
//    return NO;
//}

- (void)dealloc {
    NSLog(@"%s", __FUNCTION__);
}

@end
